#pragma once

#include <string>
#include <map>
#include <unordered_map>
#include "base/Define.h"
#include "protocol/routeinfo.pb.h"
#include "protocol/transport.pb.h"
#include <memory>
#include <shared_mutex>
#include "producer/client/MessagePublisher.h"
#include "producer/client/RouteInfoDownloader.h"
#include "codec/ProtocolCodec.h"

namespace zeroplus::mq::producer
{
    enum ProcessorType : uint8_t
    {
        DownLoader,
        Publisher
    };
    class AbstractRequestService;
    class ProducerMediator
    {
    private:
        std::shared_ptr<RouteInfoDownloader> routeInfoDownloader_;
        std::shared_ptr<MessagePublisher> messagePublisher_;

    public:
        ProducerMediator() {}

        void Notify(const ProcessorType &sender, const Bytes &data)
        {
            switch (sender)
            {
            case Publisher:
            {
                routeInfoDownloader_->InternalReceive(data.to_string());
                break;
            }
            case DownLoader:
            {
                auto response = ProtocolCodec<transport::QueryRouteInfoResponse>::Decode(data);
                std::string topic = response.topic();
                routeinfo::TopicRouteInfo topicRouteInfo = response.topic_route_info();
                messagePublisher_->InternalReceive(topic, topicRouteInfo);
                break;
            }
            default:
            {
                break;
            }
            }
        }

        void setRouteInfoDownloader(std::shared_ptr<RouteInfoDownloader> routeInfoDownloader)
        {
            routeInfoDownloader_ = routeInfoDownloader;
        }

        void setMessagePublisher(std::shared_ptr<MessagePublisher> messagePublisher)
        {
            messagePublisher_ = messagePublisher;
        }
    };
}
